package org.example.Thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class NestedThreadsExample3 {

    private static final AtomicBoolean stopAllThreads = new AtomicBoolean(false);
    private static final AtomicBoolean stopAllThreads1 = new AtomicBoolean(false);


    private static  List<Integer> list = new ArrayList<>();
    private static Integer zero = 1;
    private static Integer zero1 = 1;

    public static void main(String[] args) {


        for (int i = 0; i < 10; i++) {
            int finalI = i;
            Thread thread = new Thread(() -> {
                // 线程执行的代码逻辑
                if (finalI ==5){
                    stopAllThreads.set(true);
                }
                test("线程1",stopAllThreads);
            });


            Thread thread2 = new Thread(() -> {
                // 线程执行的代码逻辑
                test("线程2",stopAllThreads1);
            });
            thread.start(); // 启动线程
            thread2.start(); // 启动线程

            try {
                thread.join(); // 等待线程执行完毕
                thread2.join(); // 等待线程执行完毕
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("执行了几次"+(i+1));
        }


    }
    private static void test(String a,AtomicBoolean stopAllThreads){
        if (!stopAllThreads.get()) {
            System.out.println(a);
        }
    }
}